package seatiger.tomcat.catalina.core;

import seatiger.tomcat.catalina.*;
import seatiger.tomcat.catalina.connector.http.HttpRequestImpl;
import seatiger.tomcat.util.StringManager;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;

/**
 * context 容器关联的Mapper具体实现
 */
@Deprecated
public class StandardContextMapper implements Mapper {

    private static final StringManager sm = StringManager.getManager(Constants.PACKAGE_CORE);

    private StandardContext context;

    private String protocol;

    @Override
    public Container getContainer() {
        return context;
    }

    @Override
    public void setContainer(Container container) {
        if (!(container instanceof StandardContext)){
            throw new IllegalArgumentException(sm.getString("httpContextMapper.container"));
        }
        this.context = (StandardContext) container;
    }

    @Override
    public String getProtocol() {
        return protocol;
    }

    @Override
    public void setProtocol(String protocol) {
        this.protocol = protocol;
    }

    /**
     * 具体的寻找逻辑
     * @param request
     * @param update
     * @return
     */
    @Override
    public Container map(Request request, boolean update) {

        String contextPath =
                ((HttpServletRequest) request.getRequest()).getContextPath();
        String requestURI = ((HttpRequest) request).getDecodedRequestURI();
        String relativeURI = requestURI.substring(contextPath.length());

        Wrapper wrapper = null;
        String servletPath = relativeURI;
        String name = null;

        //准确的匹配
        if (wrapper == null) {
            if (!relativeURI.equals("/")){
                name = context.findServletMapping(relativeURI);
                if (name != null) {
                    wrapper = context.findChild(name);
                }
            }
        }
        //前缀匹配
        if (wrapper == null) {
            servletPath = relativeURI;
            while (true) {
                name = context.findServletMapping(servletPath + "/*");
                if (name != null)
                    wrapper = context.findChild(name);
                if (wrapper != null) {
                    break;
                }
                int slash = servletPath.lastIndexOf('/');
                if (slash < 0)
                    break;
                servletPath = servletPath.substring(0, slash);
            }
        }

        //扩展匹配
        if (wrapper == null) {
            int slash = relativeURI.lastIndexOf('/');
            if (slash >= 0) {
                String last = relativeURI.substring(slash);
                int period = last.lastIndexOf('.');
                if (period >= 0) {
                    String pattern = "*" + last.substring(period);
                    name = context.findServletMapping(pattern);
                    if (name != null) {
                        wrapper = context.findChild(name);
                    }
                }
            }
        }

        if (wrapper == null) {
            name = context.findServletMapping("/");
            if (name != null) {
                wrapper = context.findChild(name);
            }
        }

        return wrapper;
    }
}
